﻿/*
 * 作者：lovefc
 * 博客：http://lovefc.cn
 * 扣扣：1102952084 
 */
 
/** 等待元素加载完成 **/
jQuery.fn.wait = function (func, times, interval) {
    var _times = times || -1, //100次
    _interval = interval || 20, //20毫秒每次 
    _self = this,
    _selector = this.selector, //选择器
    _iIntervalID; //定时器id
    if( this.length ){ //如果已经获取到了，就直接执行函数
        func && func.call(this);
    } else {
        _iIntervalID = setInterval(function() {
            if(!_times) { //是0就退出
                clearInterval(_iIntervalID);
            }
            _times <= 0 || _times--; //如果是正数就 --
            
            _self = $(_selector); //再次选择
            if( _self.length ) { //判断是否取到
                func && func.call(_self);
                clearInterval(_iIntervalID);
            }
        }, _interval);
    }
    return this;
}

var list={
   "zhihu":{"site":"www.zhihu.com","site_name":"知乎"},
   //"weibo":{"site":"www.weibo.com","site_name":"微博"},
   "gitee":{"site":"www.gitee.com","site_name":"码云"},
   "hupu":{"site":"www.hupu.com","site_name":"虎扑"},   
   "bili":{"site":"www.bilibili.com","site_name":"B站"},
   "v2ex":{"site":"www.v2ex.com","site_name":"V2EX"},
   "hu60":{"site":"hu60.cn","site_name":"虎绿林"},
   "kanxue":{"site":"bbs.pediy.com","site_name":"看雪论坛"},
   "tieba":{"site":"tieba.baidu.com","site_name":"贴吧"}
}

function strJoin(site){
    var html = '<tr><td class="site">';
       html += '<a href="https://'+site['site']+'" target="_blank">';
       html += site['site']+'</a></td>';            
       html += '<td class="site-name">'+site['site_name']+'</td>';
	   if(site['status']==1){
           html += '<td class="site-remove"><a bs="'+site['bs']+'" class="stop_site red">关闭</a></td>';
	   }else{
		   html += '<td class="site-remove"><a bs="'+site['bs']+'" class="open_site">开启</a></td>';
	   }
	   html += '</tr>';
	return html;
}

// 运行
function start(){
    chrome.storage.sync.get(null,function(items){
	    var html = '';
        for(x in list){
		    list[x]['bs'] = x;
		    if(x in items){
			   list[x]['status'] = 1;
		    }else{
			   list[x]['status'] = 0;
		    }
            html += strJoin(list[x]);
        }
	    $('#custom').html(html);
    });
}

// 关闭站点
function stop_site(){
    $(".stop_site").on('click',function (e) {	
	   e.preventDefault(); 
	   var bs = $(this).attr('bs');
	   var dom = $(this);
	   chrome.storage.sync.remove(bs,function(){
		   console.log(bs+'关闭成功');
		   dom.attr('class','open_site');
		   dom.text('开启');
		   location.reload();
	   });
    });
}

function wait(){
    $(".stop_site").wait(function(){
	    stop_site();
    });
    $(".open_site").wait(function(){
	    open_site();
    });
}

function open_site(){
    $(".open_site").on('click',function () {
	    var bs = $(this).attr('bs');
		var dom = $(this);
	    var json = JSON.parse('{"'+bs+'":"'+bs+'"}');
        chrome.storage.sync.get(null,function(items){
		    var set_arr = items;
		    var d = Object.assign(set_arr,json);
		    chrome.storage.sync.set(d,function(){
			    console.log(bs+'开启成功');
				dom.attr('class','stop_site red');
		        dom.text('关闭');
				location.reload();
		    });
        });
    });
}

start();
wait();

/* 测试清空所有 
chrome.storage.sync.clear(function(){
	console.log('清空');
});	
*/
/*
chrome.storage.sync.get(null,function(items){
	console.log(items);
});
*/
